package guggaTests.tasks
{
	import flash.events.Event;
	
	import flexunit.framework.TestCase;
	
	import gugga.tasks.FictiveTask;
	import gugga.tasks.WrappingTask;
	import gugga.tasks.events.TaskEvent;

	public class WrappingTaskTest extends TestCase
	{
		protected var mWrappingTaskEventDispatched : Boolean = false;
		protected var mWrappedTaskEventDispatched : Boolean = false;
		protected var mWrappedTask : FictiveTask = new FictiveTask;
		protected var mWrappingTask : WrappingTask = new WrappingTask(null);
		
		
		public function WrappingTaskTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testWrappingFictiveEventRethrowing() : void
		{
			mWrappingTask.WrappedTask = mWrappedTask;
			
			testEventDispatching(TaskEvent.START, mWrappingTask.start);
			testEventDispatching(TaskEvent.INTERRUPTED, mWrappingTask.interrupt);
			
			mWrappingTask.start();
			
			testEventDispatching(TaskEvent.COMPLETED);
		}
				
		protected function testEventDispatching(aEventName : String, mOperation : Function = null) : void
		{
			mWrappingTask.addEventListener(aEventName, wrappingTaskEventFunction);
			mWrappedTask.addEventListener(aEventName, wrappedTaskEventFunction);
			
			if(mOperation != null)
			{
				assertFalse(
					"wrappingTask shouldn't dispatched event:" + aEventName ,
					mWrappingTaskEventDispatched
					);
					
				assertFalse(
					"wrappedTask shouldn't dispatched  event:" + aEventName,
					mWrappedTaskEventDispatched
					);
					
				mOperation();
			}
			
			assertTrue(
				"wrappingTask should dispatch event:" + aEventName,
				mWrappingTaskEventDispatched
				);
				
			assertTrue(
				"wrappedTask should dispatch event:" + aEventName,
				mWrappedTaskEventDispatched
				);
				
			mWrappedTaskEventDispatched = false;
			mWrappingTaskEventDispatched = false;
		}
		
		public function wrappedTaskEventFunction(aEvent : Event) : void
		{
			mWrappedTaskEventDispatched = true;
		}
		
		public function wrappingTaskEventFunction(aEvent : Event) : void
		{
			mWrappingTaskEventDispatched = true;
		}
		
	}
}